package com.okaixz.common.client;

import com.okaixz.common.client.exception.CommonErrorCode;
import com.okaixz.common.client.exception.ErrorCode;
import lombok.Data;

import java.io.Serializable;

/**
 * 后端统一返回结果
 *
 * @param <T>
 */
@Data
public class WsResult<T> implements Serializable {

    private Integer code;
    private String msg;
    private T data;
    private String token;

    public static <T> WsResult<T> success(Integer resultCode) {
        WsResult<T> result = new WsResult<T>();
        result.code = resultCode;
        return result;
    }

    public static <T> WsResult<T> success(T object, Integer resultCode) {
        WsResult<T> result = new WsResult<T>();
        result.data = object;
        result.code = resultCode;
        return result;
    }

    public static <T> WsResult<T> success(T object, Integer resultCode, String token) {
        WsResult<T> result = new WsResult<T>();
        result.data = object;
        result.code = resultCode;
        result.token = token;
        return result;
    }


    public static <T> WsResult<T> error(String msg) {
        WsResult result = new WsResult();
        result.msg = msg;
        result.code = CommonErrorCode.UNKOWN.getCode();
        return result;
    }

    public static <T> WsResult<T> error(ErrorCode commonErrorCode) {
        WsResult result = new WsResult();
        result.msg = commonErrorCode.getDesc();
        result.code = commonErrorCode.getCode();
        return result;
    }
}

